package com.donn.securitycontrolplatform.common.entity.dahua;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Author Glenn
 * @Description //TODO
 * @Date 20:55 2021/6/1
 * @Param
 * @return
 **/
@Data
@ApiModel(description = "分页DTO")
public class PageDTO<T> {

    public static final String ASC = "asc";
    public static final String DESC = "desc";
    public static final String DEFAULT_SORT = "create_date";


    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页", example = "1", required = true)
    private int limit = 1;

    /**
     * 每页显示条数
     */
    @ApiModelProperty(value = "每页显示条数", example = "1000", required = true)
    private int offset = 1000;

    /**
     * 排序字段
     */
    @ApiModelProperty(value = "排序字段")
    private String sort = DEFAULT_SORT;

    /**
     * 排序类型
     */
    @ApiModelProperty(value = "排序类型", example = ASC)
    private String sortType = ASC;

    /**
     * 默认排序字段
     */
    @ApiModelProperty(value = "默认排序字段")
    private String defSort;

    /**
     * 默认排序类型
     */
    @ApiModelProperty(value = "默认排序类型", example = ASC)
    private String defSortType = ASC;


    /**
     * 构造mybatis-plus分页page对象
     *
     * @throws
     * @Description:
     * @param: @return
     * @return: Page<T>
     */
    public Page<T> page() {
        Page<T> page = new Page<T>(limit, offset);
        if (ObjectUtils.isNotEmpty(sort.trim())) {
            if (ASC.equals(sortType)) {
                page.addOrder(OrderItem.asc(sort));
            } else {
                page.addOrder(OrderItem.desc(sort));
            }
        }
        return page;
    }


}
